package timbap.visualizer.coreplugins;

import processing.core.PApplet;
import timbap.visualizer.api.model.RecordItem;
import timbap.visualizer.api.model.TimeCode;
import timbap.visualizer.api.plugin.container.AbstractTransitionModeController;


public class GrowingSelectingModeControl extends AbstractTransitionModeController {
    
	private static final int TRANSLATION_ACCELERATION_FACTOR = 5;
	private static final int SCALE_ACCELERATION_FACTOR = 8;
	private static final int RECORD_ITEMS_FADE_OUT_ACCELERATION_FACTOR = 10;
    public static final float DEFAULT_PLAYBACK_SCALE_FACTOR = 0.65f;

    @Override
    public void timeCodeUpdated(TimeCode pTimeCode) {
        super.timeCodeUpdated(pTimeCode);

        RecordItem tSelectedRecordItem = getContext().getRecordItemManager().getSelectedRecordItem();
        
        float tCurrentFractionOfSelectionTime = getKeepAliveExpirationAsFractional();

        float tBrowsingModeZTranslation = tSelectedRecordItem.getTransformation().getZTranslation();
        float tScalingAdaptation = DEFAULT_PLAYBACK_SCALE_FACTOR * getContext().getCfg().getScreenHeight()
                                         * (getContext().getCfg().getPlaybackScaleFactor()
                                                / getContext().getCfg().getBrowsingScaleFactor());
        float zTranslation = tBrowsingModeZTranslation + ((tScalingAdaptation - tBrowsingModeZTranslation)
        		* PApplet.pow(tCurrentFractionOfSelectionTime, SCALE_ACCELERATION_FACTOR));
        
        float tBrowsingModeXTranslation = tSelectedRecordItem.getTransformation().getXTranslation();
        float tBrowsingModeYTranslation = tSelectedRecordItem.getTransformation().getYTranslation();
        float xTranslation = tBrowsingModeXTranslation - 
        	(PApplet.pow(tCurrentFractionOfSelectionTime, TRANSLATION_ACCELERATION_FACTOR) 
        			* tBrowsingModeXTranslation);
        float yTranslation = tBrowsingModeYTranslation -
        	(PApplet.pow(tCurrentFractionOfSelectionTime, TRANSLATION_ACCELERATION_FACTOR) 
        			* tBrowsingModeYTranslation);
        tSelectedRecordItem.getTransformation().setXTranslation(xTranslation);
        tSelectedRecordItem.getTransformation().setYTranslation(yTranslation);
        tSelectedRecordItem.getTransformation().setZTranslation(zTranslation);
        
        for (RecordItem tRecordItem : getContext().getRecordItemManager().getRecordItems()) {
        	if (!tRecordItem.isSelected()) {
        		float tAlphaValue = PApplet.pow(1 - tCurrentFractionOfSelectionTime, RECORD_ITEMS_FADE_OUT_ACCELERATION_FACTOR);
        		tRecordItem.getTransformation().setAlphaValue(tAlphaValue);
        	}
        }
        
    }

    
}
